package com.small.view.custview.dialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.small.R;
import com.small.interfaces.Util;

public class PopSecondAdapter extends BaseAdapter implements
		OnItemClickListener {

	List<String> list;
	private LayoutInflater mInflater;
	ViewHolder viewHolder;
	public static int mSelectItem = 0;
	private int ctag;
	Context ctx;

	public PopSecondAdapter(Context co, List<String> _list, int tag) {
		this.mInflater = LayoutInflater.from(co);
		list = _list;
		ctag = tag;
		ctx = co;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = mInflater.inflate(
					R.layout.choice_single_listpop_item2, null);
			viewHolder = new ViewHolder();
			viewHolder.viewParent = (View) convertView
					.findViewById(R.id.item_parent);
			viewHolder.mTextView = (TextView) convertView
					.findViewById(R.id.textView);
			viewHolder.mCheckBox = (CheckBox) convertView
					.findViewById(R.id.checkBox);

			convertView.setTag(viewHolder);

			// if (mCheckBoxResourceID != 0) {
			viewHolder.mCheckBox.setButtonDrawable(R.drawable.selector_checkbox2);
			// }

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

//		Log.d("mSelectItem", "" + mSelectItem);
		if (mSelectItem == position) {
			// viewHolder.viewParent.setBackgroundResource(R.color.listitem_text_pressed);
			viewHolder.mCheckBox.setChecked(true);
			//
		} else {
			// viewHolder.viewParent
			// .setBackgroundResource(R.color.listitem_color_noremal);
			viewHolder.mCheckBox.setChecked(false);
		}

		String item = (String) getItem(position);
		if (item instanceof CharSequence) {
			viewHolder.mTextView.setText((CharSequence) item);
		} else {
			viewHolder.mTextView.setText(item.toString());
		}

		return convertView;

	}

	public static class ViewHolder {
		public View viewParent;
		public TextView mTextView;
		public CheckBox mCheckBox;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
//		if (position != mSelectItem) {
			Log.d("mSelectItem", position + "");
			mSelectItem = position;
			notifyDataSetChanged();
			HashMap<String, Object> map2 = new HashMap<String, Object>();
			map2.put("position", position);
			Util.getActivityByName(ctx.getClass().getName()).onRefresh(ctag, map2);
//		}
	}
	
	public void setSelectItem(int selectId, ArrayList<String> list2) {
		mSelectItem = selectId;
		list = list2;
		notifyDataSetChanged(); 
	}
}
